<style lang="scss">
  body{
    background: #ebebeb;
  }
  .login-container{
    .top_div{
      background: #008ead;
      width: 100%;
      height: 300px;
    }
    .tou {
      background: url("../../assets/login/tou.png") no-repeat;
      width: 97px;
      height: 92px;
      position: absolute;
      top: -105px;
      left: 138px;
    }
    .hand{
      transition:all 500ms;
    }
    .left_hand {
      background: url("../../assets/login/left_hand.png") no-repeat;
      width: 32px;
      height: 37px;
      position: absolute;
      top: -56px;
      left: 145px;
    }
    .right_hand {
      background: url("../../assets/login/right_hand.png") no-repeat;
      width: 32px;
      height: 37px;
      position: absolute;
      top: -56px;
      right: -60px;
    }
    .initial_left_hand {
      background: url("../../assets/login/hand.png") no-repeat;
      width: 30px;
      height: 20px;
      position: absolute;
      top: -30px;
      left: 102px;
    }
    .initial_right_hand {
      background: url("../../assets/login/hand.png") no-repeat;
      width: 30px;
      height: 20px;
      position: absolute;
      top: -30px;
      right: -103px;
    }
    .el-card__header{
      padding-bottom: 10px;
    }
    .el-form-item{
      margin-bottom: 10px;
      .el-form-item__content{
        line-height: normal;
      }
    }
  }
</style>
<template>
  <div class="login-container" @keyup.enter="onSubmit">
    <div class="top_div"></div>
    <el-card class="box-card" shadow="hover" style="width: 400px;margin: -130px auto auto;"
             :body-style="{'padding': '10px 20px'}">
      <el-form slot="header" ref="form" :model="form" :rules="rules" :inline-message="true" size="—">
        <div style="width: 165px; height: 96px; position: absolute;">
          <div class="tou"></div>
          <div :class="handLeftCls" id="left_hand"></div>
          <div :class="handRightCls" id="right_hand"></div>
        </div>
        <el-form-item prop="username">
          <el-input v-model="form.username" prefix-icon="el-icon-user" placeholder="请输入登录名" :autofocus="true"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" @focus="pwdFocus" @blur="pwdBlur" prefix-icon="el-icon-key" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item prop="captcha">
          <el-col :span="16">
            <el-input :maxlength="4" v-model="form.captcha" placeholder="请输入验证码"></el-input>
          </el-col>
          <el-col :span="8">
            <el-image style="vertical-align: middle;border-radius: 4px;margin-left: 10px;height: 40px;width: 108px;"
                      :src="captchaUrl"
                      fit="contain"
                      alt="验证码" @click="refreshCaptcha"/>
          </el-col>
        </el-form-item>
        <el-form-item style="margin: 0">
          <el-checkbox v-model="form.checked">记住密码</el-checkbox>
        </el-form-item>
      </el-form>
      <el-row>
        <el-col :span="12">
          <el-link type="info" style="margin-top: 7px">忘记密码?</el-link>
        </el-col>
        <el-col :span="12" style="text-align: right">
          <el-link type="info" style="margin-right: 7px">注册</el-link>
          <el-button type="primary" @click="onSubmit" size="small">登录</el-button>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>
<script>
import { captchaUrl } from '_api/common'
export default {
  data () {
    return {
      form: {
        username: 'admin',
        password: 'test',
        captcha: '',
        checked: false
      },
      rules: {
        username: [
          { required: true, message: '请输入登录名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: { required: true, message: '请输入密码', trigger: 'blur' },
        captcha: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { len: 4, message: '验证码为4位数', trigger: 'blur' }
        ]
      },
      captchaUrl: '',
      handLeftCls: 'hand initial_left_hand',
      handRightCls: 'hand initial_right_hand',
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created () {
    this.refreshCaptcha()
  },
  methods: {
    onSubmit () {
      let _this = this
      _this.$refs.form.validate((valid) => {
        if (valid) {
          _this.$store.dispatch('user/login', _this.form).then(() => {
            _this.$router.push({ path: _this.redirect || '/', query: _this.otherQuery })
          }).catch(() => {
            _this.refreshCaptcha()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    refreshCaptcha () {
      let timestemp = new Date().getTime()
      this.captchaUrl = captchaUrl + '?t=' + timestemp
    },
    pwdFocus () {
      this.handLeftCls = 'hand left_hand'
      this.handRightCls = 'hand right_hand'
    },
    pwdBlur () {
      this.handLeftCls = 'initial_left_hand'
      this.handRightCls = 'initial_right_hand'
    },
    getOtherQuery (query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>
